package io.github.hadyang.leetcode.offer;

import org.junit.Test;

/** @author haoyang.shi */
public class MaxProfit {

  @Test
  public void test() {
    System.out.println(maxProfit(new int[] {9, 11, 8, 5, 7, 12, 16, 14}));
  }

  public int maxProfit(int[] nums) {
    if (nums == null || nums.length == 0) return 0;

    int min = Integer.MAX_VALUE;
    int maxProfit = 0;
    for (int i = 0; i < nums.length; i++) {
      min = Math.min(min, nums[i]);

      maxProfit = Math.max(maxProfit, nums[i] - min);
    }

    return maxProfit;
  }
}
